﻿//-----------------------------------------------------------------
//
// @(#)$Id: PostageDetails.aspx.cs,v 1.0 2013/12/13 17:22:34 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:22:34 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal.CenterDB_BasTableAdapters;
using ebay.Dal.K3DataBaseTableAdapters;

public partial class UI_Bas_PostageDetails : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        //发货方式Delivery
        T_ExpressShippingMethodsTableAdapter shippingAdapter = new T_ExpressShippingMethodsTableAdapter();
        ddlShippingMethods.DataSource = shippingAdapter.GetData();
        ddlShippingMethods.DataTextField = "Delivery";
        ddlShippingMethods.DataValueField = "Delivery";
        ddlShippingMethods.DataBind();

        //包裹类型
        T_SubMessageTableAdapter subMessageAdapter = new T_SubMessageTableAdapter();
        ddlPackageType.DataSource = subMessageAdapter.GetDataByFTypeId(10008);
        ddlPackageType.DataTextField = "FName";
        ddlPackageType.DataValueField = "FItemID";
        ddlPackageType.DataBind();
        ddlPackageType.Items.Insert(0, new ListItem("<--请选择-->", ""));    
  
        //国家
        T_ExpressShippingMethodsTableAdapter adapter = new T_ExpressShippingMethodsTableAdapter();
        ddlCountryNames.DataSource = adapter.GetData();
        ddlCountryNames.DataTextField = "CountryNames";
        ddlCountryNames.DataValueField = "CountryNames";
        ddlCountryNames.DataBind();
        ddlCountryNames.Items.Insert(0, new ListItem("<--请选择-->", ""));

    }

    static string act = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        act = Request.QueryString["act"];
        if (!IsPostBack)
        {
            switch (act)
            {
                case "I":
                    break;
                case "U":
                    {
                        this.txtId.Attributes["readonly"] = "readonly";
                        int id = int.Parse(Request.QueryString["id"]);
                        T_PostageTableAdapter adapter = new T_PostageTableAdapter();
                        DataTable dt = adapter.GetDataById(id);
                        DataRow r = dt.Rows[0];                       
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control ctr in form1.Controls)
                            {
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                    txtbox.Value = r[name].ToString();
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlbox = (HtmlSelect)ctr;
                                    string name = ddlbox.ID.Substring(3, ddlbox.ID.Length - 3);
                                    ddlbox.Value = r[name].ToString();                                    
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlist = (DropDownList)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                    ddlist.Text = r[name].ToString();
                                    if(name =="CountryNames")
                                    {
                                        this.DDLBing();                                        
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DateTime defaultDateTime = new DateTime(1900, 1, 1);
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        string userId = Session["UserId"].ToString();

        if (!string.IsNullOrEmpty(act))
        {
            switch (act)
            {
                case "I":
                    {
                        T_PostageTableAdapter adatper = new T_PostageTableAdapter();
                        int flag = (int)adatper.IsExists(int.Parse(txtId.Value));
                        if (flag == 0)
                        {
                            int val = adatper.Insert(
                                int.Parse(txtId.Value),
                                txtExpress.Value,                              
                                ddlCountryNames.SelectedValue,
                                ddlShippingMethods.SelectedValue,
                                ddlCity.SelectedValue,                              
                                ddlState.SelectedValue,
                                ddlPackageType.SelectedValue,
                                Convert.ToDecimal(txtStartingWeight.Value == "" ? "0" : txtStartingWeight.Value),
                                Convert.ToDecimal(txtEndingWeight.Value == "" ? "0" : txtEndingWeight.Value),
                                Convert.ToDecimal(txtStandardsIncreaseWeight.Value == "" ? "0" : txtStandardsIncreaseWeight.Value),
                                Convert.ToDecimal(txtBasicPostage.Value == "" ? "0" : txtBasicPostage.Value),
                                Convert.ToDecimal(txtPostageRateIncreases.Value == "" ? "0" : txtPostageRateIncreases.Value),
                                userId,
                                System.DateTime.Now,
                                "",
                                defaultDateTime
                                );
                            if (val > 0)
                            {
                                Response.Write("<script>alert('添加成功!');window.location.href='Postage.aspx'</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('此编号已经存在!..');</script>");
                        }
                    }
                    break;
                case "U":
                    {
                        int id = int.Parse(Request.QueryString["id"]);
                        T_PostageTableAdapter adapter = new T_PostageTableAdapter();
                        DataTable dt = adapter.GetDataById(id);
                        DataRow r = dt.Rows[0];
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control ctr in form1.Controls)
                            {
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);

                                    r[name] = txtbox.Value;
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlist = (HtmlSelect)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);                                   
                                    r[name] = ddlist.Value;
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlist = (DropDownList)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                    r[name] = ddlist.SelectedValue;
                                }
                                else if(ctr is TextBox )
                                {
                                    TextBox txtbox = (TextBox)ctr;
                                    string name = txtbox.ID.Substring(3,txtbox .ID .Length -3);
                                    r[name] = txtbox.Text;
                                }
                            }
                        }
                        r["Muser"] = userId;
                        r["Mtime"] = System.DateTime.Now;
                        r["Auser"] = "";
                        r["Atime"] = defaultDateTime; 
                        int val = adapter.Update(r);
                        if (val > 0)
                        {
                            Response.Write("<script>alert('修改成功!');window.location.href='Postage.aspx'</script>");
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    //绑定State,City
    protected void ddlCountryNames_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DDLBing();
    }
    public void DDLBing()
    {
        string country = ddlCountryNames.SelectedValue;
        T_PostageAddressTableAdapter adapter = new T_PostageAddressTableAdapter();
        if (!string.IsNullOrEmpty(country))
        {
            //City
            ddlCity.DataSource = adapter.GetData(country);
            ddlCity.DataTextField = "City";
            ddlCity.DataValueField = "City";
            ddlCity.DataBind();
            ddlCity.Items.Insert(0, new ListItem("<--请选择-->", ""));

            //State
            ddlState.DataSource = adapter.GetData(country);
            ddlState.DataTextField = "State";
            ddlState.DataValueField = "State";
            ddlState.DataBind();
            ddlState.Items.Insert(0, new ListItem("<--请选择-->", ""));

            if (ddlState.Items.Count == 1 || ddlCity.Items.Count == 1)
            {
                Response.Write("<script>alert('没有对该国家维护，请先维护国家列表!..');this</script>");
            }
        }
    }
}
